using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Library.CPP.Notation;

namespace OR.Lazarus.Library.CPP.Notation.Lazarus
{
	/// <summary>
	/// Defines a core Lazarus type that naming contexts will be provided for.
	/// </summary>
   public class TypeDetail : EntityBase
   {
		protected System.String mName;
		protected string mValue;
		protected string mDescription;
	
		protected Derived.NameCollection mNames;

		public TypeDetail()
		{
			mNames = new Derived.NameCollection();
			mNames.ParentEntity = this;
	
		}
	
		/// <summary>
		/// 
		/// </summary>
		public System.String Name
		{
			get
			{
				return ( mName );
			}
			set
			{
				mName = value;
			}
		}

		/// <summary>
		/// Specifies the notation that will be used by default.  This will be used if a specific context cannot be identified.
		/// </summary>
		public string Value
		{
			get
			{
				return ( mValue );
			}
			set
			{
				mValue = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public Derived.NameCollection Names
		{
			get
			{
				return ( mNames );
			}
			set
			{
				mNames = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			mName = DataUtility.GetString( pSource, "name" );
			mValue = DataUtility.GetString( pSource, "value" );
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lzn:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			
			XmlElement vNames = ( XmlElement )pSource.SelectSingleNode( "lzn:names", PersistenceConfig.Namespaces );
			
			if ( vNames != null )
			{
				mNames.LoadXml( vNames );
			}
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lzn", "type", "http://schemas.icitadel.com/Lazarus/CPP/Notation.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mName ) )
			{
				vNewElement.SetAttribute( "name", mName );
			}
					
			if ( !String.IsNullOrEmpty( mValue ) )
			{
				vNewElement.SetAttribute( "value", mValue );
			}
					
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lzn", "description", "http://schemas.icitadel.com/Lazarus/CPP/Notation.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			// Collections
			
			XmlElement	vCollection	= null;
			
			vCollection = mNames.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.TypeDetail v_oReturn = new Derived.TypeDetail();
			
			v_oReturn.Name = mName;
			v_oReturn.Value = mValue;
			v_oReturn.Description = mDescription;
			
			// FK Values

			// Collections

			foreach ( Derived.NameDetail vDetail in mNames )
			{
				v_oReturn.Names.Add( vDetail.CloneTyped() );
			}
			
			return ( v_oReturn );
		}

		public virtual Derived.TypeDetail CloneTyped()
		{
			return ( ( Derived.TypeDetail )this.Clone() );
		}
   }
}
   